package com.example.dishfo.myapplication.activity.showimgae;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.example.dishfo.myapplication.GlideApp;
import com.example.dishfo.myapplication.R;
import com.example.dishfo.myapplication.customview.toolbar.TextTitle;
import com.example.dishfo.myapplication.customview.viewpager.HackViewPager;
import com.example.dishfo.myapplication.util.ActivityUtil;
import com.example.dishfo.myapplication.util.TextUtil;
import com.github.chrisbanes.photoview.PhotoView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ShowImageActivity extends AppCompatActivity {

    public static final String URLS_LABEL="urls";
    public static final String CUR_INDEX="index";

    @BindView(R.id.viewPager) HackViewPager viewPager;
    @BindView(R.id.textTitle) TextTitle textTitle;

    private List<String> imageUrls;
    private ShowImageViewAdapter adapter;
    private int initIndex=0;

    private ListenerWorker listenerWork;

    private ViewPager.OnPageChangeListener pageChangeListener=new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            String newTitle= TextUtil.getChineseNumber(position+1);
            textTitle.setTitle(newTitle);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityUtil.setTransportStatusBar(getWindow());
        setContentView(R.layout.activity_show_image);
        ButterKnife.bind(this);
        Intent intent=getIntent();

        imageUrls=intent.getStringArrayListExtra(URLS_LABEL);
        initIndex=intent.getIntExtra(CUR_INDEX,0);

        adapter=new ShowImageViewAdapter();
        viewPager.setAdapter(adapter);

        viewPager.setCurrentItem(initIndex);
        viewPager.addOnPageChangeListener(pageChangeListener);

        textTitle.setOnClickListener(v -> {
            onBackPressed();
        });

        listenerWork=new ListenerWorker();

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction()==MotionEvent.ACTION_DOWN){
            if(textTitle.getVisibility()==View.GONE){
                Animation animation= AnimationUtils.loadAnimation(ShowImageActivity.this,
                        R.anim.view_appear);
                animation.setFillBefore(false);
                textTitle.startAnimation(animation);
                textTitle.setVisibility(View.VISIBLE);
            }
            listenerWork.updateTime();
        }

        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void onStart() {
        super.onStart();
        listenerWork.start();
    }

    @Override
    protected void onStop() {
        super.onStop();
        listenerWork.interrupt();
    }

    private class ShowImageViewAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return imageUrls==null?0:imageUrls.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view==object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            PhotoView photoView=new PhotoView(container.getContext());
            GlideApp.with(container.getContext())
                    .load(imageUrls.get(position))
                    .fitCenter()
                    .into(photoView);
            container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            return photoView;

        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
        }
    }


    private static final int SHOW=1;
    private static final int HIDE=2;

    @SuppressLint("HandlerLeak")
    private Handler timeHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what==HIDE){
                Animation animation= AnimationUtils.loadAnimation(ShowImageActivity.this,
                        R.anim.view_disapper);
                animation.setFillBefore(false);
                textTitle.startAnimation(animation);
                textTitle.setVisibility(View.GONE);
            }
        }
    };

    private class ListenerWorker extends Thread{

        //1 start else 0 stop
        private volatile int status=0;
        private volatile long startTime;

        @Override
        public void run() {
            while (!isInterrupted()){
                if(System.currentTimeMillis()-1500>startTime&&
                        textTitle.getVisibility()==View.VISIBLE){
                    updateTime();
                    Message msg=timeHandler.obtainMessage();
                    msg.what=HIDE;
                    timeHandler.sendMessage(msg);
                }
                try {
                    Thread.sleep(1700);
                } catch (InterruptedException e) {
                    Log.d("test","the thread will stop");
                }
            }
        }

      
        public void updateTime(){
            startTime=System.currentTimeMillis();
        }
    }

}

